<template>
  <el-dialog :title="action === 'add' ? '新增' : '修改'" width="40%" draggable 
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="90px">
      <el-form-item label="分配人员" prop="assigneeId">
        <user-select ref="userSelectRef" v-model="dataForm.assigneeId"></user-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话" maxlength="20" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input-number v-model.number="dataForm.priority" :min="0" :max="10" :step="1" style="width: 100%" />
      </el-form-item>
      <!--      <el-form-item label="采购状态" prop="status">
        <el-select v-model="dataForm.status" style="width: 100%;">
          <el-option label="新建" :value="0"></el-option>
          <el-option label="已分配" :value="1"></el-option>
          <el-option label="已领取" :value="2"></el-option>
          <el-option label="已完成" :value="3"></el-option>
          <el-option label="有异常" :value="4"></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="仓库" prop="wareId">
        <wareinfo-select ref="wareinfoSelectRef" v-model="dataForm.wareId"></wareinfo-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import type { FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

import WareinfoSelect from "../common/wareinfo-select.vue";
import UserSelect from "../common/user-select.vue";

// 操作：add-新增，update-修改
const action = ref("");

// 弹窗是否显示
const visible = ref(false);

// 提交状态
const submitted = ref(false);

// 表单数据
const dataForm = reactive({
  id: undefined,
  assigneeId: undefined,
  assigneeName: undefined,
  phone: "",
  priority: 0,
  status: 0,
  wareId: "",
});

// 分配人员列表
const assignee = reactive({
  dataList: [],
  loading: false,
  pageSize: 20,
});

// 数据校验规则
const dataRule = reactive({
  phone: validate.required("联系电话"),
  priority: validate.required("优先级"),
  status: validate.required("采购状态"),
  wareId: validate.required("仓库"),
  amount: validate.required("总金额"),
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 分配人员引用
const userSelectRef = ref();

// 库存引用
const wareinfoSelectRef = ref();

// 父级组件的自定义事件
const emit = defineEmits(['loadTableData']);

// 表单初始化
const initForm = async (a, data) => {
  action.value = a;
  // 新增操作
  if (a === "add") {
    visible.value = true;
    await nextTick();
    dataFormRef.value.resetFields();
    return;
  }
  // 修改操作
  dataForm.id = data.id;
  http
    .post({
      url: `/ware/purchase/info?purchaseId=${dataForm.id}`,
      method: "post",
    })
    .then(async ({ data }) => {
      if (http.success(data)) {
        visible.value = true;
        await nextTick();
        dataFormRef.value.resetFields();
        let form = data.body;
        Object.keys(form).forEach(function (key) {
          dataForm[key] = form[key];
        });
        userSelectRef.value.init(dataForm.assigneeId);
        wareinfoSelectRef.value.init(dataForm.wareId);
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: `/ware/purchase/save`,
          data: dataForm,
        }).then(({ data }) => {
          if (http.success(data, true, true)) {
            visible.value = false;
            emit("loadTableData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
